import React, { useReducer } from "react";

function reduce(state = 0, action) {
  switch (action.type) {
    case "add":
      state += 1;
      break;
    case "minus":
      state -= 1;
      break;
    default:
      throw new Error();
  }
  return state;
}
function Reducer() {
  let [state, dispatch] = useReducer(reduce, 2);
  return (
    <div>
      <button
        onClick={() => {
          dispatch({ type: "minus" });
        }}
      >
        &nbsp;-&nbsp;
      </button>
      <span>&nbsp;{state}&nbsp;</span>
      <button
        onClick={() => {
          dispatch({ type: "add" });
        }}
      >
        &nbsp;+&nbsp;
      </button>
    </div>
  );
}
export default Reducer;
